package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.home.dao.ReportDrawingAuditHistoryMapper;
import com.eastfair.home.dto.ReportDrawingAuditHistoryDTO;
import com.eastfair.home.entity.ReportDrawingAudit;
import com.eastfair.home.entity.ReportDrawingAuditHistory;
import com.eastfair.home.service.ReportDrawingAuditHistoryService;
import com.eastfair.home.vo.ReportDrawingAuditHistoryVO;
import com.eastfair.venueservice.consts.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 报馆图纸审核记录历史
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportDrawingAuditHistoryServiceImpl extends SuperServiceImpl<ReportDrawingAuditHistoryMapper, ReportDrawingAuditHistory> implements ReportDrawingAuditHistoryService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportDrawingAuditHistory> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportDrawingAuditHistory model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<ReportDrawingAuditHistoryVO> listByReportDrawingCategoryAuditHistoryId(Long reportDrawingCategoryAuditHistoryId) {
        log.info("listByReportDrawingCategoryAuditHistoryId - 通过图纸分类审核历史ID查询图纸审核历史, reportDrawingCategoryAuditHistoryId={}", reportDrawingCategoryAuditHistoryId);
        ReportDrawingAuditHistoryDTO query = new ReportDrawingAuditHistoryDTO();
        query.setReportDrawingCategoryAuditHistoryId(reportDrawingCategoryAuditHistoryId);
        List<ReportDrawingAuditHistory> list = listReportDrawingAuditHistory(query);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list.stream()
                .map(history -> BeanUtil.toBean(history, ReportDrawingAuditHistoryVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ReportDrawingAuditHistory> listReportDrawingAuditHistory(ReportDrawingAuditHistoryDTO query) {
        log.info("listReportDrawingAuditHistory - 查询报馆图纸审核历史, query={}", query);
        QueryWrapper<ReportDrawingAuditHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ReportDrawingAuditHistory::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getReportDrawingCategoryAuditHistoryId() != null,
                        ReportDrawingAuditHistory::getReportDrawingCategoryAuditHistoryId, query.getReportDrawingCategoryAuditHistoryId())
        ;
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveHistory(ReportDrawingAuditHistoryDTO reportDrawingAuditHistoryDTO) {
        ReportDrawingAuditHistory reportDrawingAuditHistory
                = BeanUtil.toBean(reportDrawingAuditHistoryDTO, ReportDrawingAuditHistory.class);
        return save(reportDrawingAuditHistory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<ReportDrawingAuditHistoryDTO> list, Long reportDrawingCategoryAuditHistoryId) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(historyDTO -> {
            historyDTO.setReportDrawingCategoryAuditHistoryId(reportDrawingCategoryAuditHistoryId);
            saveHistory(historyDTO);
        });
        return true;
    }

    @Override
    public List<ReportDrawingAuditHistoryDTO> toReportDrawingAuditHistoryDTOList(List<ReportDrawingAudit> reportDrawingAuditList) {
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
        return reportDrawingAuditList
                .stream()
                .map(reportDrawingAudit -> {
                    ReportDrawingAuditHistoryDTO reportDrawingAuditHistoryDTO = BeanUtil.toBean(reportDrawingAudit, ReportDrawingAuditHistoryDTO.class, copyOptions);
                    reportDrawingAuditHistoryDTO.setReportDrawingAuditId(reportDrawingAudit.getId());
                    return reportDrawingAuditHistoryDTO;
                })
                .collect(Collectors.toList());
    }
}
